<?php (defined('BASEPATH')) OR exit('No direct script access allowed');

/**
 * @author Anh Nguyen <duyanhnguyen@vccorp.vn>
 * @copyright 2012
 */

class Category extends MX_Controller
{

    public function __construct()
    {
        
        $this->load->library('Admin_Layout');
        $this->load->library('Minifier');
        $this->load->model('product_model');
    }
    /**
     *list category 
     */
    public function index()
    {
        /*
         * load header + left menu
         */
        $mainType = 1;
        $otherType = 2;
        $nameMainProduct = $this->product_model->getProduct($mainType, 0);
        
        $listMainProduct = $this->product_model->getGroupProduct($mainType);

        $nameOtherProduct = $this->product_model->getProduct($otherType, 0);
        $listOtherProduct = $this->product_model->getGroupProduct($otherType);
        
        $this->admin_layout->header();
        $this->admin_layout->leftMenu();
        
        $data = array();
        $data['nameMainProduct'] = $nameMainProduct;
        
        $data['listMainProduct'] = $listMainProduct;
        $data['nameOtherProduct'] = $nameOtherProduct;
        $data['listOtherProduct'] = $listOtherProduct;

        $this->load->view('category/category_view', $data);
    }
    /**
    * add category
    * 
    * 
    */
    public function add()
    {
        /*
         * load header + left menu
         */
        $pass = 0;
        $mainType = 1;
        $otherType = 2;
        if($this->input->post('submit')) {
            //insert database
            $categoryType = $this->input->post('category_type');
            $name_vn = $this->input->post('name_vn');
            $name_en = $this->input->post('name_en');
            $name_jp = $this->input->post('name_jp');
            $order = $this->input->post('order');
            
            $data = array();
            $data['name_vn'] = $name_vn;
            $data['name_en'] = $name_en;
            $data['name_jp'] = $name_jp;
            $data['type'] = $categoryType;
            $data['parent_id'] = $categoryType;
            $data['order_id'] = $order;
            //add category form
            $this->product_model->insert($data);
            $pass = 1;
            redirect('admin/category/index');
            
        }
        
        $this->admin_layout->header();
        $this->admin_layout->leftMenu();
        
        $data = array();
        
        $data['pass'] = $pass;

        $this->load->view('category/add_category_view', $data);
    }
    /**
    * edit category
    * 
    * 
    */
    public function edit()
    {
        /*
         * load header + left menu
         */
        $pass = 0;
        $mainType = 1;
        $otherType = 2;
        $productId = $this->uri->segment(4, 0);
        
        $arrayCategory = $this->product_model->getProductById($productId);
        
        if($this->input->post('submit')) {
            //insert database
            $categoryType = $this->input->post('category_type');
            $name_vn = $this->input->post('name_vn');
            $name_en = $this->input->post('name_en');
            $name_jp = $this->input->post('name_jp');
            $order = $this->input->post('order');
            if($productId == 1) {
                $type = 1;
                $categoryType = 0;
                $order = 0;
            } else if($productId == 2) {
                $type = 2;
                $categoryType = 0;
                $order = 0;
            } else {
                $type = $categoryType;
            }
            
            $data = array();
            $data['id'] = $productId;
            $data['name_vn'] = $name_vn;
            $data['name_en'] = $name_en;
            $data['name_jp'] = $name_jp;
            $data['type'] = $type;
            $data['parent_id'] = $categoryType;
            $data['order_id'] = $order;
            //add category form
            $this->product_model->update($data);
            
            redirect('admin/category/index');
            
        }
        
        $this->admin_layout->header();
        $this->admin_layout->leftMenu();
        
        $data = array();
        
        $data['product'] = $arrayCategory;

        $this->load->view('category/edit_category_view', $data);
    }
    
    public function delete()
    {
        $productId = $this->uri->segment(4, 0);
        $this->product_model->delete($productId);
        redirect('admin/category/index');
    }
    public function listProduct($productId = '', $page = '')
    {
        if ($productId == '') {
            show_404();
        }
        $productId = intval($productId);

        $page = ($page != '') ? $page : 0;
        
        $this->load->model('item_model');
        $listItem = $this->item_model->getListItemByCategoryId($productId);
        //pagination------------------------------
        $this->load->library('pagination');

        $config['base_url'] = site_url().'admin/category/listProduct/'.$productId.'/';
        $config['total_rows'] = count($listItem);
        $config['per_page'] = 2;
        $config['use_page_numbers'] = TRUE;
        $config['full_tag_open'] = '<p>';
        $config['full_tag_close'] = '</p>';
        $config['cur_tag_open'] = '<b>';
        $config['cur_tag_close'] = '</b>';
        $config['cur_page'] = $page;
        $config['uri_segment'] = 5;

        $this->pagination->initialize($config); 
        
        $result = $this->item_model->getAllItemByCategoryId($productId, $config['per_page'], $page);

        //end pagination--------------------------
        $this->admin_layout->header();
        $this->admin_layout->leftMenu();
        
        $data['listItem'] = $result;
        $data['productId'] = $productId;

        $this->load->view('category/list_item_view', $data);
    }

}

?>
